package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceShadowDataVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * iot设备管理Controller
 *
 * @author cxl
 * @date 2025-10-28
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "iot设备管理相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /*产品同步*/
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "iot设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/syncProductList")
    @ApiOperation("同步iot设备管理到redis")
    public R syncProductList() {
        deviceService.syncProductList();
        return R.ok();
    }

    /*查询所有产品*/
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping(value = "/allProduct")
    @ApiOperation("查询所有产品")
    public R getAllProduct() {
        List<ProductVo> allProduct = deviceService.getAllProduct();
        return R.ok(allProduct);
    }

    /*新增iot设备管理*/
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "iot设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    @ApiOperation("新增iot设备管理")
    public R add(@RequestBody DeviceDto dto) {
        deviceService.insertDevice(dto);
        return R.ok();
    }

    /*查询iot设备管理列表*/

    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取iot设备管理列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }
    /*获取iot设备管理详细信息*/

    @GetMapping("/{iotId}")
    @ApiOperation("获取iot设备管理详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public R<DeviceDetailVo> getInfo(@PathVariable("iotId") String iotId) {
        return R.ok(deviceService.selectDeviceByIotId(iotId));
    }

    /*
    获取设备上报的数据
    * queryServiceProperties
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("获取设备上报的数据")
    public R<List<DeviceShadowDataVo>> getDeviceShadowData(@PathVariable("iotId") String iotId) {
        return R.ok(deviceService.getDeviceShadowData(iotId));
    }

    @PutMapping
    @ApiOperation("修改iot设备管理")
    public R edit(@RequestBody Device device) {
        deviceService.updateDevice(device);
        return R.ok();
    }


}